import store from '@/store';
function loadingEvent(commit: any) {
  let num = 0;

  const add = () => {
    if (num === 0) {
      commit({ type: 'setLoading', payload: true });
    }
    num = num + 1;
  };

  const sub = () => {
    num = num - 1 > 0 ? num - 1 : 0;
    if (num === 0) {
      commit({ type: 'setLoading', payload: false });
    }
  };

  const reset = () => {
    num = 0;
    if (num === 0) {
      commit({ type: 'setLoading', payload: false });
    }
  };

  return {
    add,
    sub,
    reset,
  };
}

export const loading = loadingEvent(store.commit);
